#ifndef CMD_CONSOLE_READER_H
#define CMD_CONSOLE_READER_H

#include "cmd_reader.h"
#include "cmd_user_session.h"

#include <iostream>
#include <boost/bind.hpp>

#ifdef WIN32
#include <conio.h>
#else 
#include <termios.h>
#include <unistd.h>
#endif //

namespace vpm 
{
	namespace cmo 
	{
		class CmdConsoleReader : public CmdReader 
		{
		public:

			CmdConsoleReader():CmdReader()
			{
				m_cmdUserSession.reset(new CmdUserSession(m_cmdView));
				CmdUserSessionAccess::attach_char_writer(*m_cmdUserSession,boost::bind(&CmdConsoleReader::write_char,this, _1)); 
				CmdUserSessionAccess::attach_string_writer(*m_cmdUserSession,boost::bind(&CmdConsoleReader::write_string,this, _1));
			}

			virtual char read_char()
			{
#ifdef WIN32
				return _getch();
#else 
				char buf[2];
				::read(0,buf,1);
				return buf[0]; 
#endif //
			}

			virtual std::string read_string()   //same as readline 
			{
				char tmpBuf[1024]; 
				std::cin.getline(tmpBuf,sizeof(tmpBuf));
				return std::string(tmpBuf);
			}

			virtual void write_char(char ch) 
			{
#ifdef WIN32
				putchar(ch);
#else 
				write(2,&ch,1);
#endif //
			}

			virtual void write_string(const char * pStr) 
			{
				char ch ;
				while ((ch = *pStr++) )
				{
#ifdef WIN32
					putchar(ch);
#else 
					write(2,&ch,1);
#endif //
				}
			}

			
		};
	}
}


#endif // 

